package com.dangwen.proto.test;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.InvalidProtocolBufferException;
import org.eclipse.paho.client.mqttv3.*;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class DeviceCommandSubscriber implements MqttCallback {

    public static void main(String[] args) {
        String broker = "tcp://47.120.58.197:1883";
        String clientId = "JavaClient";
        try {
            MqttClient mqttClient = new MqttClient(broker, clientId);
            mqttClient.setCallback(new DeviceCommandSubscriber());
            mqttClient.connect();

            // 订阅设备上传命令的主题
            String commandTopic = "/feed/register"; // 假设设备上传命令的主题为 "device/command"
            mqttClient.subscribe(commandTopic);

            System.out.println("开始监听设备上传命令...");
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("连接丢失：" + throwable.getMessage());
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws InvalidProtocolBufferException, UnsupportedEncodingException {
        byte[] payload = mqttMessage.getPayload();

        String s = bytesToHex(mqttMessage.getPayload());
        System.out.println(s);

        String text = new String(mqttMessage.getPayload(), StandardCharsets.ISO_8859_1); // 使用指定字符编码解码成字符串
        System.out.println(text);
        System.out.println("------------2");
        String text2 = new String(mqttMessage.getPayload(), StandardCharsets.US_ASCII); // 使用指定字符编码解码成字符串
        System.out.println(text2);
        System.out.println("------------3");
        String text3 = new String(mqttMessage.getPayload(), StandardCharsets.UTF_16); // 使用指定字符编码解码成字符串
        System.out.println(text3);
        System.out.println("------------4");
        String text4 = new String(mqttMessage.getPayload(), "GBK"); // 使用指定字符编码解码成字符串
        System.out.println(text4);
//        Gateway_Register gateway_register = Gateway_Register.parseFrom(mqttMessage.getPayload());
//        System.out.println("--"+JSON.toJSONString(gateway_register));
//        Gateway_Register gateway_register = Gateway_Register.parseFrom(mqttMessage.getPayload());
//        One_Term_Sys_Param one_term_sys_param = One_Term_Sys_Param.parseFrom(mqttMessage.getPayload());
//        System.out.println("收到设备上传命令，主题：" + topic + "，命令内容：" + JSON.toJSONString(s));
//        System.out.println("收到设备上传命令，主题：" + topic + "，命令内容：" + new String(mqttMessage.getPayload()));
        // 在这里处理设备上传的命令
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        // 消息传递完成
    }

    // 将字节数组转换为十六进制字符串
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(String.format("%02X", b)); // 将每个字节转换为两位十六进制数值
        }
        return hexString.toString();
    }
}

